package lium.lbs.lbsim.activity

import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import com.buz.hjcdriver.bean.WalletIndexResultBean
import com.buz.hjcdriver.bean.WalletRecordResultBean
import com.lmlibrary.Constants
import com.lmlibrary.bean.ResponseBean
import lium.lbs.lbsim.utils.callbck.DialogCallback
import com.lzy.okgo.model.Response
import com.readystatesoftware.chuck.internal.support.DividerItemDecoration
import kotlinx.android.synthetic.main.activity_wallet.*
import kotlinx.android.synthetic.main.activity_wallet_head.*
import lium.lbs.lbsim.R
import lium.lbs.lbsim.adapter.WalletAdapter
import lium.lbs.lbsim.jgchat.utils.photovideo.takevideo.utils.LogUtils
import java.util.HashMap

class WalletActivity : BaseActivity() {

    lateinit var adapter: WalletAdapter
    var page: Int = 1

    override fun getLayoutId(): Int {
        return R.layout.activity_wallet
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitleWithBack("钱包")
        initRecyclerView()
        addOnClickListeners(R.id.right_bar)
        getDataList()
        findViewById<ImageView>(R.id.right_img).setImageResource(R.drawable.icon_bankcard)
        findViewById<ImageView>(R.id.right_img).visibility = View.VISIBLE
    }

    fun initRecyclerView() {
        //初始化adapter
        adapter = WalletAdapter()
        val layoutManager = LinearLayoutManager(this)
        recyclerView.layoutManager = layoutManager
        recyclerView.adapter = adapter
        headviewlayout.addView(LayoutInflater.from(this).inflate(R.layout.activity_wallet_head, null))
        tixian.setOnClickListener(this)
        wallet_charge.setOnClickListener(this)
        //设置emptyView
        adapter.emptyView = LayoutInflater.from(this).inflate(R.layout.view_list_nodata, null, false)
        recyclerView.addItemDecoration(DividerItemDecoration(this, layoutManager.orientation, false)) //分割线
        adapter.disableLoadMoreIfNotFullPage(recyclerView) //关闭BRAVE的自动加载功能 改成 refreshLayout的上拉加载
        refreshLayout.setOnRefreshListener {
            page = 1
            getDataList()
        }
        refreshLayout.setOnLoadMoreListener {
            getDataList()
        }
    }

    private fun getDataList() {
        val param = HashMap<String, String>()
        param["pageNumber"] = page.toString()
        param["pageSize"] = Constants.PAGE_SIZE.toString()
        postData("/member/selectYe", param, object : DialogCallback<ResponseBean<WalletIndexResultBean>>(this) {
            override fun onSuccess(response: Response<ResponseBean<WalletIndexResultBean>>) {
                tv_money.text = response.body().data.money + ""
                setDataList(response.body().data.selectYe)
            }
        })

    }


    private fun setDataList(list: List<WalletRecordResultBean>) {
        if (page == 1) {
            adapter!!.data.clear()
        }
        refreshLayout.finishRefresh()
        refreshLayout.finishLoadMore()
        list?.let {
            adapter!!.data.addAll(list)
        }
        if (list == null || list.isEmpty()) {
            refreshLayout.finishLoadMoreWithNoMoreData()  //没有数据
        }
        adapter!!.notifyDataSetChanged()
        page++

    }

    override fun getNetWorkData() {
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when (v!!.id) {
            R.id.tixian -> {
                val intent = Intent(this, WalletWithdrawalsActivity::class.java)
                startActivityForResult(intent, 100)
            }
            R.id.right_bar -> {
                val intent = Intent(this, BankCardListActivity::class.java)
                startActivityForResult(intent, 100)
            }
            R.id.wallet_charge -> {
                val intent = Intent(this, ChargeActivity::class.java)
                startActivityForResult(intent, 250)
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        LogUtils.e("body--requestCode" + requestCode + "--resultCode--" + resultCode)
        if (requestCode == 100 && resultCode == 200) {
            page = 1
            getDataList()
        }
        if (requestCode == 250) {
            page = 1
            getDataList()
        }
    }
}